package com.szty.lib_pen

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.util.Log
import android.widget.ImageView
import androidx.core.view.isVisible
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.sjh.baseutils.loge
import com.sjh.baseutils.logi
import com.sjh.lib_app.bean.QuestionData
import com.szty.lib_pen.oidbluetooth.DotUtils
import com.szty.lib_pen.surfaceview.DrawView
import com.tqltech.tqlpencomm.bean.Dot

/**
 *    author : SunJH
 *    date   : 2021/5/14 0014
 *    desc   : 一些当前模块的工具方法
 */

/**
 * 截取图片中的答题信息
 */
object Utils {
    /**
     * 显示学生答题情况的图片
     * 学生答题情况展示，需要从完整的答题卡里面去截图
     * 学生作答图片       需要从作答图片里面截取 整体图片是 points_image_url
     *                      截取时，坐标从图片左上角开始算
     *                                   split_startx   x轴开始点
     *                                   split_starty   y轴开始点
     *                                   split_height   y轴结束点
     *                                   split_width    x轴结束点
     */
    fun initStudentQuestionImg(context: Context, imgShowPoint: ImageView, data: QuestionData) {
        if (data.split_height == 0f || data.split_width == 0f || data.split_startx == 0f || data.split_starty == 0f || data.points_image_url == "") {
            imgShowPoint.isVisible = false
            return
        }
        if (data.questionImg == null) {
            urlTransformBitmap(context, data.points_image_url) {
                val bitmap = cropBitmap(
                    it,
                    data.split_width,
                    data.split_height,
                    data.split_startx,
                    data.split_starty
                )
                if (bitmap != null) {
                    imgShowPoint.isVisible = true
                    data.questionImg = bitmap
                    Glide.with(context).load(bitmap).into(imgShowPoint)
                }
                it.recycle()
            }

        } else {
            Glide.with(context).load(data.questionImg).into(imgShowPoint)
        }
    }
     fun processEachDot(dot: Dot,drawView:DrawView) {
        if (dot.force < 0) return
        val BookID = dot.BookID
        val PageID = dot.PageID
        if (BookID < 0 || PageID < 0) return //防止笔连接不切页

        //拼接dot点
        val x: Float = DotUtils.joiningTogether(dot.x, dot.fx)
        val y: Float = 194.94f-DotUtils.joiningTogether(dot.y, dot.fy)
        val pointX: Float = DotUtils.getPoint(
            y,
            drawView.bG_WIDTH,
            drawView.papeR_WIDTH,
            DotUtils.DIST_PERUNIT
        )
        val pointY: Float = DotUtils.getPoint(
            x,
            drawView.bG_HEIGHT,
            drawView.papeR_HEIGHT,
            DotUtils.DIST_PERUNIT
        )
         Log.e("SunJH","X:${y} pointX:${pointX}")
         Log.e("SunJH","Y:${x} pointY:${pointY}")

        val pointZ = dot.force
        if (pointZ > 0) {
            drawView.processDot(pointX, pointY, pointZ, dot.type)
        } else if (dot.type == Dot.DotType.PEN_UP) {
            drawView.processDot(pointX, pointY, pointZ, Dot.DotType.PEN_UP)
        }
    }


    /**
     * 将url转换成bitmap
     */
    fun urlTransformBitmap(
        context: Context,
        pointsImageUrl: String,
        isUseCache: Boolean = true,
        result: (Bitmap) -> Unit
    ) {
        Glide.with(context).asBitmap()
            .skipMemoryCache(!isUseCache)
            .diskCacheStrategy(
                if (isUseCache) DiskCacheStrategy.DATA else DiskCacheStrategy.NONE
            ).load(pointsImageUrl).into(
                object : CustomTarget<Bitmap>() {
                    override fun onResourceReady(
                        resource: Bitmap,
                        transition: Transition<in Bitmap>?
                    ) {
                        val copy = resource.copy(Bitmap.Config.RGB_565, true)
                        result.invoke(copy)
                    }

                    override fun onLoadCleared(placeholder: Drawable?) {}
                })


    }

    fun cropBitmap(
        resource: Bitmap,
        splitWidth: Float,
        splitHeight: Float,
        splitStartx: Float,
        splitStarty: Float
    ): Bitmap? {
        val width = resource.width
        val height = resource.height
        if (width < splitStartx + splitWidth) return null
        if (height < splitStarty + splitHeight) return null
        return Bitmap.createBitmap(
            resource,
            splitStartx.toInt(),
            splitStarty.toInt(),
            splitWidth.toInt(),
            splitHeight.toInt(),
            null,
            false
        )
    }

}